package org.nokatag.tagjava;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;

import org.nokatag.password.AESPlus;
import org.nokatag.system.Concest;

public class StatChart  extends BodyNokaTag {

	private static final long serialVersionUID = 5068586299780134408L;
	private  HttpServletRequest request = null;
	private  JspWriter jspOut =null;
	private String id=null;
	private String width="600";
	private String height="350";
	private String xmlpath=null;//xml文件路径
	private String sfun=null;//调用服务器方法
	private String type=null;
	private String pars = null;//参数/参数 [{name:'',value:''},{}]
    
	public int doStartTag()throws JspException{
		request = (HttpServletRequest) pageContext.getRequest();
		jspOut= pageContext.getOut();
		return EVAL_BODY_BUFFERED;
	}
    public int doEndTag() throws JspException{
    	BodyContent body = this.getBodyContent();
		String     content = (null==body?null:body.getString());
	    outpage(bodyStr(content));
	    if(null!=body)
	    	body.clearBody();
	    return SKIP_BODY;
    }
    //-----------------------------------------------------------
    public void outpage(String content){
    	 try {
 			jspOut.write(content);
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
    }
	//-------------------------------------------------------------
    public String bodyStr(String content){
    	String basePath = basepath(request);
		StringBuilder body = new StringBuilder();
		if(xmlpath!=null && xmlpath.trim().length()>4){
			xmlpath = xmlpath.replaceAll("(?i)\\$\\[rooturl\\]/", basePath);
			if(xmlpath.indexOf("?")==-1)
				xmlpath+="?";
		}
		Map<String,String> typeHashMap = new HashMap<String,String>();
		//-------------------------------------------------------------
		typeHashMap.put("Area2D", "Area2D.swf");
		typeHashMap.put("Bar2D", "Bar2D.swf");
		typeHashMap.put("Bubble", "Bubble.swf");
		typeHashMap.put("Column2D", "Column2D.swf");
		typeHashMap.put("Column3D", "Column3D.swf");
		typeHashMap.put("Doughnut2D", "Doughnut2D.swf");
		typeHashMap.put("Doughnut3D", "Doughnut3D.swf");
		typeHashMap.put("FCExporter", "FCExporter.swf");
		typeHashMap.put("Line", "Line.swf");
		typeHashMap.put("Marimekko", "Marimekko.swf");
		typeHashMap.put("MSArea", "MSArea.swf");
		typeHashMap.put("MSBar2D", "MSBar2D.swf");
		typeHashMap.put("MSBar3D", "MSBar3D.swf");
		typeHashMap.put("MSColumn2D", "MSColumn2D.swf");
		typeHashMap.put("MSColumn3D", "MSColumn3D.swf");
		typeHashMap.put("MSColumn3DLineDY", "MSColumn3DLineDY.swf");
		typeHashMap.put("MSColumnLine3D", "MSColumnLine3D.swf");
		typeHashMap.put("MSCombi2D", "MSCombi2D.swf");
		typeHashMap.put("MSCombi3D", "MSCombi3D.swf");
		typeHashMap.put("MSCombiDY2D", "MSCombiDY2D.swf");
		typeHashMap.put("MSLine", "MSLine.swf");
		typeHashMap.put("MSStackedColumn2D", "MSStackedColumn2D.swf");
		typeHashMap.put("MSStackedColumn2DLineDY", "MSStackedColumn2DLineDY.swf");
		typeHashMap.put("Pareto2D", "Pareto2D.swf");
		typeHashMap.put("Pareto3D", "Pareto3D.swf");
		typeHashMap.put("Pie2D", "Pie2D.swf");
		typeHashMap.put("Pie3D", "Pie3D.swf");
		typeHashMap.put("Scatter", "Scatter.swf");
		typeHashMap.put("ScrollArea2D", "ScrollArea2D.swf");
		typeHashMap.put("ScrollColumn2D", "ScrollColumn2D.swf");
		typeHashMap.put("ScrollCombi2D", "ScrollCombi2D.swf");
		typeHashMap.put("ScrollCombiDY2D", "ScrollCombiDY2D.swf");
		typeHashMap.put("ScrollLine2D", "ScrollLine2D.swf");
		typeHashMap.put("ScrollStackedColumn2D", "ScrollStackedColumn2D.swf");
		typeHashMap.put("SSGrid", "SSGrid.swf");
		typeHashMap.put("StackedArea2D", "StackedArea2D.swf");
		typeHashMap.put("StackedBar2D", "StackedBar2D.swf");
		typeHashMap.put("StackedBar3D", "StackedBar3D.swf");
		typeHashMap.put("StackedColumn2D", "StackedColumn2D.swf");
		typeHashMap.put("StackedColumn2DLine", "StackedColumn2DLine.swf");
		typeHashMap.put("StackedColumn3D", "StackedColumn3D.swf");
		typeHashMap.put("StackedColumn3DLine", "StackedColumn3DLine.swf");
		typeHashMap.put("StackedColumn3DLineDY", "StackedColumn3DLineDY.swf");
		typeHashMap.put("ZoomLine", "ZoomLine.swf");
		//-------------------------------------------------------------
		typeHashMap.put("AngularGauge", "AngularGauge.swf");
		typeHashMap.put("Bulb", "Bulb.swf");
		typeHashMap.put("Cylinder", "Cylinder.swf");
		typeHashMap.put("DrawingPad", "DrawingPad.swf");
		typeHashMap.put("FCExporter", "FCExporter.swf");
		typeHashMap.put("Funnel", "Funnel.swf");
		typeHashMap.put("Gantt", "Gantt.swf");
		typeHashMap.put("HBullet", "HBullet.swf");
		typeHashMap.put("HLED", "HLED.swf");
		typeHashMap.put("HLinearGauge", "HLinearGauge.swf");
		typeHashMap.put("Pyramid", "Pyramid.swf");
		typeHashMap.put("RealTimeArea", "RealTimeArea.swf");
		typeHashMap.put("RealTimeColumn", "RealTimeColumn.swf");
		typeHashMap.put("RealTimeLine", "RealTimeLine.swf");
		typeHashMap.put("RealTimeLineDY", "RealTimeLineDY.swf");
		typeHashMap.put("RealTimeStackedArea", "RealTimeStackedArea.swf");
		typeHashMap.put("RealTimeStackedColumn", "RealTimeStackedColumn.swf");
		typeHashMap.put("SparkColumn", "SparkColumn.swf");
		typeHashMap.put("SparkLine", "SparkLine.swf");
		typeHashMap.put("SparkWinLoss", "SparkWinLoss.swf");
		typeHashMap.put("Thermometer", "Thermometer.swf");
		typeHashMap.put("VBullet", "VBullet.swf");
		typeHashMap.put("VLED", "VLED.swf");
		body.append(potjs(request));
		body.append(StatChartJS(request));
		//-----------------------------------------------------------------
		body.append("<div id=\""+id+"_div\">");
		body.append("<div id=\""+id+"_dav\" style=\"display: none;\" ><div id=\""+id+"_data\">"+(content==null?"":content)+"</div></div>");
		body.append("<div id=\""+id+"\" align=\"center\"><img alt=\"loading....\" src=\""+basePath+"/nokatag/tablegrid/images/mt_load.gif\"></img></div>");
		body.append("</div>");
		body.append("<script type=\"text/javascript\">\n");
        body.append("Event.observe(document, 'dom:loaded', function() {");
		body.append("var ncharts"+id+" = {");
		body.append("id:'"+id+"',");//id
		body.append("rurl:'"+basePath+"',");//根url
		body.append("width:'"+width+"',");//宽
		body.append("height:'"+height+"',");//高
		if(null!=xmlpath)
			body.append("xmlpath:'"+xmlpath+"',");//xml path
		if(null!=sfun)
			body.append("sfun:'"+AESPlus.encrypt(sfun, Concest.SQLKEY)+"',");//server function
		if(null!=sfun)
			body.append("parsva:"+pars+",");//参数
		body.append("type:'"+typeHashMap.get(type)+"'");//type
		body.append("};");
		body.append("var ncha"+id+" = new NStatCharts(ncharts"+id+");");
		body.append("ncha"+id+".show();");
		body.append("});");
		body.append("</script>");
		return body.toString();
    }
    //-------------------------------------------------------------
	public String getHeight() {
		return height;
	}


	public void setHeight(String height) {
		this.height = height;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getWidth() {
		return width;
	}


	public void setWidth(String width) {
		this.width = width;
	}


	public String getXmlpath() {
		return xmlpath;
	}


	public void setXmlpath(String xmlpath) {
		this.xmlpath = xmlpath;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String getSfun() {
		return sfun;
	}

	public void setSfun(String sfun) {
		this.sfun = sfun;
	}

	public String getPars() {
		return pars;
	}

	public void setPars(String pars) {
		this.pars = pars;
	}
}
